package com.propertypoint.presentation.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.propertypoint.persitence.dao.impl.AdminDAO;
import com.propertypoint.persitence.dao.impl.AdminDAOImpl;
import com.propertypoint.presentation.model.Advertiesment;

/**
 * Servlet implementation class AdminLoginServlet
 */
@WebServlet("/adminlogin")
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String username = request.getParameter("UserName");
		String password = request.getParameter("Password");
		
		PrintWriter out = response.getWriter();
		if (username.equals("admin") && password.equals("admin")){
			session.setAttribute("id", "admin");
			session.setAttribute("admin_login_status", "true");
			
			//TODO: get data from DB and forward it to the admin.jsp page to display
			//probably need to put the DB operation in a separate JAVA file
			
			//List<Advertiesment> list2 = new ArrayList<Advertiesment>();
			AdminDAO adminDAO = new AdminDAOImpl();
			List<Advertiesment> ads = adminDAO.getAds();
			
			
			request.setAttribute("mylist", ads);
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/dashboard.jsp"); 
			rd.forward(request, response);
			//out.println("here we go");
		}else{
			out.println("login error");
		}
	}

}
